import logging
import colorlog

class Loging:
    
    def __init__(self):
        # 获取logger对象
        self.logger = logging.getLogger(None)
        # 避免重复打印日志
        self.logger.handlers = []
        # 指定最低日志级别：（critical > error > warning > info > debug）
        self.logger.setLevel(logging.DEBUG)

        # 日志格化字符串
        console_fmt = '%(log_color)s%(asctime)s %(levelname)s: %(message)s'

        # 控制台输出不同级别日志颜色设置
        color_config = {
            'DEBUG': 'cyan',
            'INFO': 'green',
            'WARNING': 'yellow',
            'ERROR': 'red',
            'CRITICAL': 'purple',
        }

        console_formatter = colorlog.ColoredFormatter(fmt=console_fmt,log_colors=color_config)

        # 输出到控制台
        console_handler = logging.StreamHandler()

        # 设置日志格式
        console_handler.setFormatter(console_formatter)

        # logger添加处理器
        self.logger.addHandler(console_handler)
    
    def debug(self, log):
        self.logger.debug(log)
    
    def info(self, log):
        self.logger.info(log)

    def warning(self, log):
        self.logger.warning(log)

    def error(self, log):
        self.logger.error(log)

    def critical(self, log):
        self.logger.critical(log)